package cn.jishiyu11.xeqb.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView

import cn.jishiyu11.xeqb.R

/**
 * Created by vvguoliang on 2017/6/28.
 * 弹出框 list view 适配器
 */

class DialogListView(context: Context, private val listString: List<Map<String, Any>>) : BaseAdapter() {
    private val mInflater: LayoutInflater

    init {
        mInflater = LayoutInflater.from(context)
    }

    override fun getCount(): Int {
        return listString.size
    }

    override fun getItem(position: Int): Any {
        return listString[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    @SuppressLint("InflateParams")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val viewHolder: ViewHolder
        if (convertView == null) {
            viewHolder = ViewHolder()
            convertView = mInflater.inflate(R.layout.dialog_listview_item, null)
            viewHolder.dialog_listview_item_checkBox = convertView!!.findViewById(R.id.dialog_listview_item_checkBox)
            viewHolder.dialog_listview_item_textview = convertView.findViewById(R.id.dialog_listview_item_textview)
            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }
        viewHolder.dialog_listview_item_textview!!.text = listString[position]["name"].toString()
        if (listString[position]["boolean"] == "1") {
            viewHolder.dialog_listview_item_checkBox!!.setImageResource(R.mipmap.ic_no_selected)
        } else {
            viewHolder.dialog_listview_item_checkBox!!.setImageResource(R.mipmap.ic_selected)
        }
        return convertView
    }

    internal inner class ViewHolder {
        var dialog_listview_item_checkBox: ImageView? = null
        var dialog_listview_item_textview: TextView? = null
    }
}
